/*
 *   Copyright (C) 2010  Jens Kieselbach
 * 
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 * 
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 * 
 *   You may obtain a copy of the License at http://www.gnu.org/licenses/gpl.html
 */

package de.kieselbach.io

import de.kieselbach.swing.*
import javax.swing.*
import javax.swing.filechooser.*

class TextFile {

    String encoding
    String suffixDescription
    def suffixes = []

    File lastDirectory
    File selectedFile

    TextFile() {

    }

    File choose(boolean forsave = false, Closure overridecheck = null) {

        selectedFile = null
        def chooser = new JFileChooser(lastDirectory)
        chooser.fileSelectionMode = JFileChooser.FILES_ONLY
        chooser.multiSelectionEnabled = false

        if(suffixes != null && suffixes.size() > 0) {
            chooser.fileFilter = new FileNameExtensionFilter(suffixDescription, suffixes as String[]);
        }

        if(forsave) {
            if(chooser.showSaveDialog() == JFileChooser.APPROVE_OPTION) {
                selectedFile = chooser.selectedFile

                if(selectedFile.exists() && overridecheck != null) {
                    if(!overridecheck(selectedFile)) {
                        selectedFile = null
                    }
                }
            }
        }
        else if(chooser.showOpenDialog() == JFileChooser.APPROVE_OPTION) {
            selectedFile = chooser.selectedFile
        }

        if(selectedFile != null) {
            lastDirectory = selectedFile.parentFile
        }

        selectedFile
    }

    String open() {

        if(selectedFile == null) {
            throw new IOException('there\'s no file selected');
        }

        def buffer = new StringBuffer()

        def reader = {input ->
            def line
            while((line = input.readLine()) != null) {
                buffer.append(line)
                buffer.append('\n')
            }
        }

        if(encoding) {
            selectedFile.withReader(encoding, reader)
        }
        else {
            selectedFile.withReader(reader)
        }

        buffer.toString()
    }

    void save(String text) {

        if(selectedFile == null) {
            throw new IOException('there\'s no file selected');
        }

        if(text == null) {
            text = ''
        }

        def writer = {output ->
            text.eachLine() {line ->
                output.println(line)
            }
        }
        
        if(encoding) {
            selectedFile.withPrintWriter(encoding, writer)
        }
        else {
            selectedFile.withPrintWriter(writer)
        }
    }
}

